package com.lyj.sc.Proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @Author: liyangjing
 * @Date: 2022/03/17/22:20
 * @Description:
 */
public class jdkproxy implements InvocationHandler {

    private Object target;
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("===================="+method.getName()+"===========");
        System.out.println("JDK动态代理开始！！！");
        Object invoke = method.invoke(target, args);
        System.out.println("JDK动态代理结束！！");

        return invoke;
    }

    /**
     * 获取代理对象
     **/
    private Object newProxyInstance(Object targetObject){
        // 目标对象赋值
        this.target = targetObject;
        // 返回代理对象
        return Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), this);
    }

    public static void main(String[] args) {
        // 获取代理对象
        ISayHello sayHello = (ISayHello) (new jdkproxy().newProxyInstance(new SayHelloImpl()));
        sayHello.sayHello();
    }
}
